package kevin.newmybatis.mybatis.proxy;

import kevin.newmybatis.mybatis.annotation.Select;
import kevin.newmybatis.mybatis.session.SqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MapperProxy implements InvocationHandler {

    private SqlSession sqlSession;

    public MapperProxy(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    /**
     * 实现注解式查询
     * @param proxy
     * @param method
     * @param args
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //获取方法上绑定的注解sql
        Select annotations = method.getAnnotation(Select.class);
        if (annotations != null) {


            //通过methodName 获取对应sql语句
//            String sql = MyConfiguration.UserMapperXML.sqlMap.get(method.getName());


            //获取注解sql，获取参数,获取method返回值类型执行查询操作
            return sqlSession.selectone(annotations.value(), String.valueOf(args[0]),method.getReturnType());
        }


        return method.invoke(proxy, args);
    }
}
